<?php
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');

class VideoAction extends AdministratorAction{
	/**
	 * 教练状态 
	 *  0 申请中
	 *  1审核通过
	 *  -1 被禁用
	 *  -2 不通过
	 */
	public function _initialize() {
		$this->pageTitle['VideoList'] = '所有资讯';
		$this->pageTitle['VideoType'] = '资讯类型';
		$this->pageTitle['addVideo'] = '添加资讯';
		$this->pageTitle['addVideoType'] = '添加类型';
		parent::_initialize();
	}
	//所有教练界面
	public function VideoList(){
		$this->_listpk = 'vid';
		$_REQUEST['tabHash'] = 'VideoList';
		// 初始化约单列表管理菜单
		$this->_initVideoListAdminMenu('VideoList');
		
		$listData = $this->_getVideoList('20', $map, 'VideoList');
		
		//dump($listData);exit;
		$this->pageButton[] = array('title'=>"搜索视频",'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	
	public function VideoType(){
		$this->_listpk = 'tid';
		$_REQUEST['tabHash'] = 'VideoType';
		// 初始化约单列表管理菜单
		$this->_initVideoListAdminMenu('VideoType');
		
		$listData = $this->_getVideoList('20', $map, 'VideoType');
		//dump($listData);exit;
// 		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	
	//课程展示界面
	private function _initVideoListAdminMenu($type) {
		// tab选项
		if($type == "VideoList" || $type == "addVideo"){
			$this->pageTab[] = array('title'=>"所有视频",'tabHash'=>'VideoList','url'=>U('admin/Video/VideoList'));
			$this->pageTab[] = array('title'=>"增加视频",'tabHash'=>'addVideo','url'=>U('admin/Video/addVideo'));
			
			$this->opt["is_display"] = array("0"=>"未展示",1=>"展示中");
			$type_id = array();
			foreach (M("Video_type")->select() as $k =>$v){
				$type_id[$v['tid']] =$v["type_name"];
			}
			$this->opt['tid'] =$type_id;
			$this->searchKey = array("vid","tid",'title','is_display',array('ctime','ctime1'));
			$this->pageKeyList =array("vid","tid","pic",'title','video_url','is_display','ctime',"DOACTION");
		}elseif($type == "VideoType"){
			$this->pageTab[] = array('title'=>"视频类型",'tabHash'=>'VideoType','url'=>U('admin/Video/VideoType'));
			$this->pageTab[] = array('title'=>"增加类型",'tabHash'=>'addVideoType','url'=>U('admin/Video/addVideoType'));
			
			$this->searchKey = array("tid",'type_name','is_display');
			$this->pageKeyList =array("tid",'type_name','is_display',"DOACTION");
		}
	
			
	
	
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
	}
	
	/**
	 * 解析约单列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getVideoList($limit = 20, $map = array(), $type = 'VideoList') {
		
		if($type == "VideoList"){
			// 设置列表主键
			$this->_listpk = 'vid';
			// 取用户列表
			$listData = model('Video')->getVideoList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['ctime'] = date('Y-m-d H:i:s',$listData['data'][$k]['ctime']);
				
				$listData['data'][$k]['is_display'] = $listData['data'][$k]['is_display']==0?"否":"是";
				$uid = $listData['data'][$k]['uid'];
				$listData['data'][$k]['tid'] = M("Video_type")->where("tid = ".$listData['data'][$k]['tid'])->find()["type_name"];
				//图片处理	

				$image = D('attach')->where('attach_id='.$listData['data'][$k]['pic'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['pic'] = getImageUrl($image_url);
				$listData['data'][$k]['pic'] = "<img style='height:100px;' src='".$listData['data'][$k]['pic']."'"." />";
	
				$listData['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Video/editVideo',array('vid'=>$v['vid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= $listData['data'][$k]['is_display']=="是"?'<a href="javascript:void(0)" onclick="admin.notDisplayVideo(\''.$v['vid'].'\')">'."不显示".'</a> - ':'<a href="javascript:void(0)" onclick="admin.displayVideo(\''.$v['vid'].'\')">'."显示".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelVideo(\''.$v['vid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
					
			}
		}elseif ($type == "VideoType"){
			// 设置列表主键
			$this->_listpk = 'tid';
			// 取用户列表
			$listData["data"] = M("Video_type")->select();
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['is_display'] = $listData['data'][$k]['is_display']==0?"否":"是";
				$listData['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Video/editVideoType',array('tid'=>$v['tid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= $listData['data'][$k]['is_display']=="是"?'<a href="javascript:void(0)" onclick="admin.notDisplayVideoType(\''.$v['tid'].'\')">'."不显示".'</a> - ':'<a href="javascript:void(0)" onclick="admin.displayVideoType(\''.$v['tid'].'\')">'."显示".'</a> - ';
				
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelVideoType(\''.$v['tid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
		}
		
		return $listData;
	}
	
	//添加操作
	public function addVideo(){
		
		$this->assign('pageTitle','添加资讯 ');
		$_REQUEST['tabHash'] = 'addVideo';
		$this->_initVideoListAdminMenu('addVideo');
	
		
		$this->pageKeyList = array("tid","pic",'title','video_url','is_display');
		$type_id = array();
		foreach (M("Video_type")->select() as $k =>$v){
			$type_id[$v['tid']] =$v["type_name"]; 
		}
		$this->opt['tid'] =$type_id;
		$this->opt['is_display'] = array(1=>"显示",0=>"不显示");
		
		$this->notEmpty = array("tid",'title','profile',"content","pic");
		$this->savePostUrl = U('admin/Video/doAddVideo');
		$this->onsubmit = 'admin.addVideoSubmitCheck(this)';
		$this->displayConfig();
	}
	//添加教练
	public function doAddVideo(){
		//dump($_REQUEST);
		$Video = M("Video");
		$Video->tid = t($_REQUEST["tid"]);
		$Video->title = t($_REQUEST["title"]);
		$video_url = $this->testUrl($_REQUEST["video_url"]);
		$Video->video_url = $video_url;
		$Video->is_display = t($_REQUEST["is_display"]);
		$Video->ctime = time();
		$Video->pic = t($_REQUEST["pic"]);
		if($Video->add()){
			$this->assign('jumpUrl', U('admin/Video/VideoList'));
			$this->success("添加成功");
			}else{
				$this->error($Video->getError());
			}
	}
	
	public function addVideoType(){

		$this->assign('pageTitle','添加资讯类型 ');
		$_REQUEST['tabHash'] = 'addVideoType';
		$this->_initVideoListAdminMenu('addVideoType');
		
		$this->pageKeyList = array("type_name","is_display");
		$this->opt['is_display'] = array(1=>"显示",0=>"不显示");
		
		$this->notEmpty = array("type_name");
		$this->savePostUrl = U('admin/Video/doAddVideoType');
		$this->onsubmit = 'admin.addNewsTypeSubmitCheck(this)';
		$this->displayConfig();
	}
	public function doAddVideoType(){
		//dump($_REQUEST);
		$Video = M("Video_type");
		$Video->type_name = t($_REQUEST["type_name"]);
		$Video->is_display = t($_REQUEST["is_display"]);
		if($Video->add()){
			$this->assign('jumpUrl', U('admin/Video/VideoType'));
			$this->success("添加成功");
		}else{
			$this->error($Video->getError());
		}
	}
	//编辑视频banner
	public function VideoBanner(){

		$this->assign('pageTitle','编辑视频Banner');
			
		$this->pageKeyList = array("pic",'title','video_url');
		$this->opt['tid'] =M("Video_type")->select();
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$Video = model('Video')->where('vid=0')->find();
		if($Video['pic']){
			$image = D('attach')->where('attach_id='.$Video['pic'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$Video['pic_url'] = getImageUrl($image_url);
		}
		$type_id = array();
		foreach (M("Video_type")->select() as $k =>$v){
			$type_id[$v['tid']] =$v["type_name"];
		}
		$this->opt['tid'] =$type_id;
		$this->opt['is_display'] = array(1=>"显示",0=>"不显示");
		
		$this->notEmpty = array('title','video_url',"is_display");
			
		$this->savePostUrl = U('admin/Video/doEditVideo',array("vid"=>0));
		//$this->onsubmit = 'admin.addVideoSubmitCheck(this)';
		$this->displayConfig($Video);
	}
	//编辑操作
		public function editVideo(){

			$this->assign('pageTitle','编辑视频');
			
			$this->pageKeyList = array("tid","pic",'title','video_url',"is_display");
			$this->opt['tid'] =M("Video_type")->select();
			//$this->opt['essence'] = array('1'=>'是','0'=>'否');
			$Video = model('Video')->where('vid='.$_GET['vid'])->find();
			if($Video['pic']){
				$image = D('attach')->where('attach_id='.$Video['pic'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$Video['pic_url'] = getImageUrl($image_url);
			}
				$type_id = array();
			foreach (M("Video_type")->select() as $k =>$v){
				$type_id[$v['tid']] =$v["type_name"]; 
			}
			$this->opt['tid'] =$type_id;
			$this->opt['is_display'] = array(1=>"显示",0=>"不显示");
		
			$this->notEmpty = array("tid",'title','video_url',"is_display");
			
			$this->savePostUrl = U('admin/Video/doEditVideo',array("vid"=>$_GET['vid']));
			$this->onsubmit = 'admin.addVideoSubmitCheck(this)';
			$this->displayConfig($Video);
		}
		public function doEditVideo(){
			//dump($_REQUEST);exit;
			$vid = t($_REQUEST["vid"]);
			$Video = M("Video");
			isset($_REQUEST["tid"]) && $Video->tid = t($_REQUEST["tid"]);
			$Video->title = t($_REQUEST["title"]);
			$Video->video_url = $this->testUrl($_REQUEST["video_url"]);
			$Video->is_display = t($_REQUEST["is_display"]);
			$Video->pic = t($_REQUEST["pic"]);
			if($Video->where("vid = $vid")->save()){
				$this->assign('jumpUrl', U('admin/Video/VideoList'));
				$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
			}else{
// 				echo $Video->getLastSql();
// 				dump($Video->getError());exit;
				$this->error($Video->getError());
			}
		}
		public function editVideoType(){
			$this->assign('pageTitle','编辑类型');
				
			$this->pageKeyList = array("type_name","is_display");
			
			//$this->opt['essence'] = array('1'=>'是','0'=>'否');
			$Video = M('Video_type')->where('tid='.$_GET['tid'])->find();

			$this->opt['is_display'] = array(1=>"显示",0=>"不显示");
			
			$this->notEmpty = array("type_name","is_display");
				
			$this->savePostUrl = U('admin/Video/doEditVideoType',array("tid"=>$_GET['tid']));
			$this->onsubmit = 'admin.addNewsTypeSubmitCheck(this)';
			$this->displayConfig($Video);
		}
		public function doEditVideoType(){
			//dump($_REQUEST);
			$Video = M("Video_type");
			$Video->type_name = t($_REQUEST["type_name"]);
			$Video->is_display = t($_REQUEST["is_display"]);
			if($Video->where("tid = ".$_GET["tid"])->save()){
				$this->assign('jumpUrl', U('admin/Video/VideoType'));
				$this->success("添加成功");
			}else{
				$this->error($Video->getError());
			}
		}
	//显示与删除操作
	public function doNotDisplayVideo(){
		$id = t($_REQUEST["vid"]);
		$Video = M("Video");
		$Video->is_display = 0;
		if ($Video->where("vid = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		
		echo json_encode($return);exit();
	}
	public function dodisplayVideo(){
		$id = t($_REQUEST["vid"]);
		$Video = M("Video");
		$Video->is_display = 1;
		if ($Video->where("vid = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   =$Video->getDbError();
		}
		echo json_encode($return);exit();
	}
	public function donotDisplayVideoType(){
		$id = t($_REQUEST["tid"]);
		$Video = M("Video_type");
		$Video->is_display = 0;
		if ($Video->where("tid = $id")->save()){
			
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	public function dodisplayVideoType(){
		$id = t($_REQUEST["tid"]);
		$Video = M("Video_type");
		$Video->is_display = 1;
		if ($Video->where("tid = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = $Video->getDbError();
		}
		echo json_encode($return);exit();
	}
	public function dotrueDelVideo(){
		$id = t($_REQUEST["vid"]);
		$Video = M("Video");
		if ($Video->where("vid = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = $Video->getDbError();
		}
		echo json_encode($return);exit();
	}
	public function dotrueDelVideoType(){
		$id = t($_REQUEST["tid"]);
		$Video = M("Video_type");
		if ($Video->where("tid = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	public function testUrl($url){
		if(substr($url, 0, 4) == "http"){
			$return =$url;
		}else{
			$search = '/(src|width|height)=([\"|\'])?(.*?)(?(2)\2|\s)/is';
			preg_match_all($search, $url , $r);
			$return = $r[3][2];
		}
		if($return==null){
			$return = $url;
		}
		return $return;
	}
}